//
//  WeiboModel.m
//  HCWeibo
//
//  Created by gj on 15/12/10.
//  Copyright © 2015年 www.iphonetrain.com 无限互联. All rights reserved.
//

#import "WeiboModel.h"

@implementation WeiboModel

- (NSDictionary*)attributeMapDictionary{
    NSDictionary *mapAtt = @{
                             @"createDate":@"created_at",
                             @"weiboId":@"id",
                             @"text":@"text",
                             @"source":@"source",
                             @"favorited":@"favorited",
                             @"thumbnailImage":@"thumbnail_pic",
                             @"bmiddlelImage":@"bmiddle_pic",
                             @"originalImage":@"original_pic",
                             @"geo":@"geo",
                             @"repostsCount":@"reposts_count",
                             @"commentsCount":@"comments_count",
                             @"weiboIdStr":@"idstr"
                             };
    return mapAtt;
}

- (void)setAttributes:(NSDictionary*)dataDic{
    //  1)Override super method.
    [super setAttributes:dataDic];
    
    //  2)Analyze user model.
    NSDictionary *userDic = dataDic[@"user"];
    self.userModel = [[UserModel alloc] initWithDataDic:userDic];
    
    //  3)Repost weibo.
    NSDictionary *repostWeiboDic = dataDic[@"retweeted_status"];
    if (repostWeiboDic != nil) {
        self.repostWeiboModel = [[WeiboModel alloc] initWithDataDic:repostWeiboDic];
        
        //  Do some extra setting for repost user's name.
        NSString *reposterName = self.repostWeiboModel.userModel.screen_name;
        NSString *repostText = [NSString stringWithFormat:@"@%@:%@",
                                reposterName,
                                self.repostWeiboModel.text];
        
        self.repostWeiboModel.text = repostText;
    }
    
    //  4)Extra modify for source text.
    if (self.source != nil) {
        //  1>Search all source text.
        NSString *regex = @">.+<";
        NSArray *regexArray = [self.source componentsMatchedByRegex:regex];
        
        //  2>Change source text into its subString.
        if (regexArray.count != 0) {
            NSString *source = regexArray[0];
            NSRange subRange = NSMakeRange(1, source.length - 2);
            NSString *subString = [source substringWithRange:subRange];
            self.source = [NSString stringWithFormat:@"%@", subString];
        }
    }
    
    //  5)Extra modify for emoji.
    //  1>Search all text in emoji.
    NSString *regexEmoji = @"\\[\\w+\\]";
    NSArray *emojiItems = [self.text componentsMatchedByRegex:regexEmoji];
    
    //  2>Find the corresponding image through text.
    NSString *emojiPath = [[NSBundle mainBundle] pathForResource:kEmojiFileName
                                                         ofType:nil];
    NSArray *emojiArray = [NSArray arrayWithContentsOfFile:emojiPath];
    
    //  3>Edit the emoji text.
    for (NSString *emoji in emojiItems) {
        NSString *predicateString = [NSString stringWithFormat:@"self.chs = '%@'", emoji];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
        NSArray *filterEmoji = [emojiArray filteredArrayUsingPredicate:predicate];
        if (filterEmoji.count > 0) {
            NSDictionary *emojiDic = filterEmoji[0];
            //  Have found the emoji name.
            NSString *imageName = emojiDic[@"png"];
        
            //  Edit the text.
            NSString *replaceString = [NSString stringWithFormat:@"<image url = '%@'>", imageName];
            
            //  Replacing the text.
            self.text = [self.text stringByReplacingOccurrencesOfString:emoji
                                                             withString:replaceString];
        }
    }
}

@end
